class ChartDataProcessor {
	constructor() {
		this.chartDataArr = [];
	}

	getChartDataByType(type, data) {
		if (type === 'ring' || type === 'pie') {
			return {
				series: [{
					data: data.map(item => ({
						name: item.name,
						value: item.value
					}))
				}]
			};
		}

		return this.transformedData(data, type);
	}

	transformedData(data, type = 'line') {
		const accumulator = {
			categories: [],
			series: []
		};

		data.forEach(item => {
			if (!accumulator.categories.includes(item.name)) {
				accumulator.categories.push(item.name);
			}

			let series = accumulator.series.find(s => s.name === item.seriesName);
			if (!series) {
				series = {
					name: item.seriesName,
					data: new Array(accumulator.categories.length).fill(0)
				};
				accumulator.series.push(series);
			}

			const index = accumulator.categories.indexOf(item.name);
			series.data[index] = item.value;
		});

		// 根据图表类型可能需要调整返回的格式  
		if ((type === 'column' || type === 'bar') && accumulator.series.length > 1) {
			// 可能需要调整数据格式以符合特定库的要求  
			// 例如，有些库要求柱状图的每个系列都包含完整的类别数组  
			accumulator.series = accumulator.series.map(series => ({
				...series,
				data: series.data.map((_, index) => ({
					name: accumulator.categories[index],
					value: series.data[index]
				}))
			}));
		}

		return accumulator;
	}

	getServerData(data) {
		if (!this.isEmpty(data)) {
			Object.keys(data).forEach(key => {
				if (!this.isEmpty(data[key])) {
					const chartsType = data[key][0]['type'];
					const backData = this.getChartDataByType(chartsType, data[key]);
					this.chartDataArr.push({
						chartType: chartsType,
						chartData: backData
					});
				}
			});
		}
	}

	isEmpty(value) {
		if (typeof value === 'undefined' || value === null) {
			return true;
		}

		if (typeof value === 'string' && value.trim() === '') {
			return true;
		}

		if (Array.isArray(value) && value.length === 0) {
			return true;
		}

		if (typeof value === 'object' && Object.keys(value).length === 0) {
			return true;
		}

		return false;
	}

	chartDataArr = []
}

export default ChartDataProcessor